<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-input placeholder="单号" title="请输入单号进行模糊匹配" v-model="pageForm.orderNo"
                      @keyup.enter.native="pageData" size="medium">
            </el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-input placeholder="用户名或手机号或昵称" title="请输入用户名或手机号或昵称进行模糊匹配" v-model="pageForm.searchKeyword"
                      @keyup.enter.native="pageData" size="medium">
            </el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.status"
                       filterable
                       size="medium"
                       placeholder="请选择状态">
                <el-option v-for="item in statusData"
                           :value="item.val"
                           :label="item.name">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.channel"
                       filterable
                       size="medium"
                       placeholder="请选择充值通道">
                <el-option v-for="item in channelData"
                           :value="item.val"
                           :label="item.name">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <div class="btn-group tool-button mt5">
            <el-button size="medium" @click="openAdd">
                <i class="ti-plus"></i>
                充值
            </el-button>
        </div>
    </el-row>

    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                :default-sort="{prop: 'opAt', order: 'descending'}">
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column label="订单号" header-align="left" prop="no"
                             :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column label="用户名" header-align="left" prop="userName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="昵称" header-align="left" prop="nickName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="手机号" header-align="left" prop="phone"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="金额" header-align="left" prop="amount"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="付款人" header-align="left" prop="sign"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.payer==null?'未输入':scope.row.payer}}
                </template>
            </el-table-column>
            <el-table-column label="附言" header-align="left" prop="sign"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.sign==null?'无':scope.row.sign}}
                </template>
            </el-table-column>

            <el-table-column label="收款账户" header-align="left" prop="accountName"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.accountName==null?'无':scope.row.accountName}}
                </template>
            </el-table-column>

            <el-table-column label="收款账号" header-align="left" prop="cardNo"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.cardNo==null?'无':scope.row.cardNo}}
                </template>
            </el-table-column>

            <el-table-column sortable label="创建时间" header-align="center" align="center" prop="ctAt">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>

            <el-table-column label="方式" header-align="left" prop="channel"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{channelFormat(scope.row.channel)}}
                </template>
            </el-table-column>
            <el-table-column
                    prop="userOnline"
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand" v-if="scope.row.status == 1">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{sure:true,id:scope.row.id}">
                                通过
                            </el-dropdown-item>
                            <el-dropdown-item :command="{sure:false,id:scope.row.id}">
                                拒绝
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    {{scope.row.status==-1?"已取消"
                    :scope.row.status==0?"待转账"
                    :scope.row.status==2?"已完成"
                    :scope.row.status==3?"未通过"
                    :""}}
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <el-dialog
            title="充值"
            :visible.sync="addDialogVisible"
            width="40%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="name" label="用户">
                <el-select
                        v-model="formData.toUser"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="请输入手机号码或用户昵称"
                        :remote-method="getAccounts"
                        :loading="loading">
                    <el-option
                            v-for="item in options"
                            :key="item.userId"
                            :label="item.label"
                            :value="item.userId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="code" label="金额">
                <el-input maxlength="100" placeholder="金额"
                          v-model="formData.amount"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAdd">确 定</el-button>
                  </span>
    </el-dialog>

</div>
<script>
    function getParams(key) {
        var result = {};
        var paramStr = encodeURI(window.document.location.search);
        if (paramStr) {
            paramStr = paramStr.substring(1);
            var param4Map = paramStr.split('&');
            for (var p = 0; p < param4Map.length; p++) {
                result[param4Map[p].split('=')[0]] = unescape(param4Map[p].split('=')[1]);
            }
        }
        let resultElement = result[key];
        return resultElement === undefined ? "" : resultElement;
    }

    new Vue({
        el: "#app",
        data: function () {
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        var threeMonths = moment().add(-3, 'months').valueOf();
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
                            || time.getTime() < threeMonths;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                statusData: [{
                    val: '',
                    name: '全部状态'
                }, {
                    val: -1,
                    name: '取消'
                }, {
                    val: 0,
                    name: '待确认'
                }, {
                    val: 1,
                    name: '待到账'
                }, {
                    val: 2,
                    name: '成功'
                }, {
                    val: 3,
                    name: '失败'
                }],
                channelData: [],
                addDialogVisible: false,
                editDialogVisible: false,
                selectData: [],
                searchDate: [],
                pageForm: {
                    orderNo: getParams("no"),
                    searchKeyword: "",
                    searchDate: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending",
                },
                tableData: [],
                formData: {
                    id: "",
                },
                formRules: {
                    toUser: [
                        {required: true, message: '充值对象', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '金额', trigger: 'blur'}
                    ]
                },
                options: [],
                loading: false
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if(self.searchDate){
                    self.pageForm.searchDate = self.searchDate.toString();
                }else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/user/account/recharge/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            channelFormat: function (val) {
                for (let channelDataKey in this.channelData) {
                    if(this.channelData[channelDataKey].val === val){
                        return this.channelData[channelDataKey].name;
                    }
                }
            },
            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            openAdd: function () {
                var self = this;
                self.addDialogVisible = true;
                self.formData = {
                    id: ""
                };//打开新增窗口,表单先清空
                if (self.$refs["addForm"])
                    self.$refs["addForm"].resetFields();
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/user/account/recharge/add";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code === 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/cms/link/class/editDo";
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            delMore: function () {
                var self = this;
                if (self.selectData.length == 0) {
                    self.$message({
                        message: "请选择消息",
                        type: 'warning'
                    });
                    return false;
                }
                var ids = [];
                self.selectData.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要删除选中的 ' + ids.length + ' 条数据？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" == a) {//确认后再执行
                            $.post(base + "/platform/cms/link/class/delete", {ids: ids.toString()}, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                let self = this;
                $.post(base + "/platform/user/account/recharge/sure", command, function (data) {
                    if (data.code == 0) {
                        self.pageData()
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            getAccounts: function (str) {
                var _this = this;
                $.post(base + "/platform/user/manager/searchAccount", {key: str}, function (data) {
                    _this.options = data.code == 0 ? data.data.list.map(ite => {
                            // ite.label =ite.nickName+'#<span style="color: darkblue">'+ite.phone+'</span>$<span style="color: darkred">'+ite.balance+'</span>'
                            ite.label = ite.nickName + '#' + ite.phone + '$' + ite.balance
                            return ite
                        }
                    ) : [];
                }, "json");
            },
            getChannelList: function () {
                let self = this;
                $.post(base + "/platform/user/account/recharge/channelList", function (data) {
                    if (data.code === 0) {
                        self.channelData = data.data;
                        self.channelData.unshift({
                            val: '',
                            name: '全部通道'
                        })
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            }
        },
        created: function () {
            this.getChannelList();
            this.pageData();
        }
    });
</script>
<!--#
}
#-->